#!/bin/bash
# $Id$

# desc: find block devices and optional file in it
# param: 1:{usb,removable,all} 2:[file-to-find]
# return: return dev name(s), if 2nd argument given, just print that dev name.
#		return 

tmpmnt="$(mktemp -d /tmp/tmp.XXXXXX)"
block_dev_list=""

arg1="$1"
arg2="$2"

shopt -s nullglob

# param: 1:{usb,removable,all}
list_devs()
{
		local d r dp
		for d in /sys/block/[hs][dr][a-z0-99]* ; do
			case $1 in
				"removable")
					r=$(cat ${d}/removable)
					if [ $r = 1 ]; then 
						block_dev_list[$((++n))]="$d"
						# and partitions
						for dp in ${d}/$(basename ${d})* ; do
							block_dev_list[$((++n))]="$(basename $dp)"
						done
					fi
					;;
				"usb")
					if lsscsi -t |awk '/usb/{print $5}'|grep -q "^/dev/"$(basename $d)"$" ; then
						block_dev_list[$((++n))]="$d"
						# and partitions
						for dp in ${d}/$(basename ${d})* ; do
							block_dev_list[$((++n))]="$(basename $dp)"
						done
					fi
					;;
				"all")
					block_dev_list[$((++n))]="$d"
					# and partitions
					for dp in ${d}/$(basename ${d})* ; do
						block_dev_list[$((++n))]="$(basename $dp)"
					done
					;;
			esac
		done

		mountable_devs "${block_dev_list[*]/\/sys\/block\//}"
}

# param: 1:{dev-list}
# return: mountable devs
mountable_devs()
{
	local arg1="$1"
	local d

	for d in $arg1 ; do
		mount -o ro /dev/${d} $tmpmnt 2>/dev/null || continue
		umount ${tmpmnt} 2>/dev/null
		echo -n "$d "
	done
}

# desc: find file in given devices
# param: 1:{list-of-block-devices-to-scan} 2:{file-to-find}
# return: first device that has file
find_file()
{
	local arg1="$1"
	local arg2="$2"

	for m in ${arg1} ; do
		mount -o ro /dev/${m} $tmpmnt &>/dev/null
		if [ -e ${tmpmnt}/${arg2} ]; then
			echo -n "$m "
		fi
		umount $tmpmnt &>/dev/null
	done
	return 1
}

# param: 1:{list-of-whole-block-devices-to-id} 2:{usb,removable,all}
# notes: if 1:usb=id_usb_<dev>,removable=id_removable_<dev>,all=id_all_<dev> to /var/tmp
id_dev ()
{
	local V
	local M
	local S
	local tmpdir="/var/tmp"
	local syspath=/sys/block
	local arg1="$1"
	local arg2="$2"
	
	for u in $arg1 ; do
		V="$(< $syspath/$(echo $u|sed 's/[1-9]//')/device/vendor)" &>/dev/null
		M="$(< $syspath/$(echo $u|sed 's/[1-9]//')/device/model)" &>/dev/null
		S="$(fdisk -l /dev/${u} 2>/dev/null | awk '/GB|MB/{print $3,$4}'|tr -d , )" &>/dev/null
		#
		echo "Device: $u" > $tmpdir/id_${arg2}_${u}
		echo "Brand: $V" >> $tmpdir/id_${arg2}_${u}
		echo "Model: $M" >> $tmpdir/id_${arg2}_${u}
		echo "Size: ${S}" >> $tmpdir/id_${arg2}_${u}
	done
}

case $arg1 in
				"usb")
					ul="$(list_devs usb)"
					if [ -n "$arg2" ]; then
						ul=$(find_file "$ul" $arg2)
						id_dev "$ul" $arg1
						echo $ul
					fi
					id_dev "$ul" $arg1
					echo $ul
					;;
				"removable")
					rl="$(list_devs removable)"
					if [ -n "$arg2" ]; then
						rl=$(find_file "$rl" $arg2)
						id_dev "$rl" $arg1
						echo $rl
					fi
					id_dev "$rl" $arg1
					echo $rl
					;;
				"all")
					al="$(list_devs all)"
					if [ -n "$arg2" ]; then
						al=$(find_file "$al" $arg2)
						id_dev "$al" $arg1
						echo $al
					fi
					id_dev "$al" $arg1
					echo $al
					;;
esac

rm -rf $tmpmnt

# vim: ts=2
# findfile-indev.sh: end 
